from functools import wraps

from flask import render_template, request, flash, redirect, url_for, session
from flask_login import login_required, current_user, login_user, logout_user

from app import app,login_manager
from app.model import *
from flask import jsonify, request
from db import db
import uuid
from flask import render_template


@app.route('/insertEquipmentFault', methods=['POST'])
def insertEquipmentFault():
    if request.method == 'POST':
        # sequence_id = request.form.get('sequence_id')
        sequence_name = request.form.get('sequence_name')
        equipment_id = request.form.get('equipment_id')
        failure_date = request.form.get('failure_date')
        fault_message = request.form.get('fault_message')
        person = request.form.get('person')
        random_uuid = uuid.uuid4()
        new_equipment_fault = Equipment_fault(sequence_id=random_uuid, sequence_name=sequence_name,
                                              equipment_id=equipment_id, failure_date=failure_date,
                                              fault_message=fault_message, person=person)
        try:
            db.session.add(new_equipment_fault)
            db.session.commit()
            flash('Your account has been created! You are now able to log in', 'success')
            return jsonify('OK')
        except Exception as e:
            flash('Username already exists. Please try a different one.', 'danger')
            return jsonify(str(e))


@app.route('/deleteEquipmentFault', methods=['POST'])
def deleteEquipmentFault():
    if request.method == 'POST':
        sequence_id = request.form.get('sequence_id')
        equipment_fault = Equipment_fault.query.filter_by(sequence_id=sequence_id).first()
        if equipment_fault:
            db.session.delete(equipment_fault)
            # 提交事务
            db.session.commit()
            return jsonify("删除成功")
        else:
            return jsonify("删除失败")


@app.route('/updateEquipmentFault', methods=['POST'])
def updateEquipmentFault():
    if request.method == 'POST':
        sequence_id = request.form.get('sequence_id')
        sequence_name = request.form.get('sequence_name')
        equipment_id = request.form.get('equipment_id')
        failure_date = request.form.get('failure_date')
        fault_message = request.form.get('fault_message')
        person = request.form.get('person')
        equipment_fault = Equipment_fault.query.filter_by(sequence_id=sequence_id).first()
        if equipment_fault:

            equipment_fault.sequence_id = sequence_id
            equipment_fault.sequence_name = sequence_name
            equipment_fault.equipment_id = equipment_id
            equipment_fault.failure_date = failure_date
            equipment_fault.fault_message = fault_message
            equipment_fault.person = person
            db.session.commit()
            return jsonify("更新成功")
        else:
            return jsonify("数据有误，更新失败")


# @app.route('/selectEquipmentFault', methods=['GET'])
def selectEquipmentFault():
    if request.method == 'GET':
        page = request.args.get('page', 1, type=int)
        per_page = request.args.get('per_page', 10, type=int)

        # 进行分页查询
        equipmentFaults = Equipment_fault.query.paginate(page=page, per_page=per_page)

        equipmentFaults_list = []
        for equipmentFault in equipmentFaults.items:
            print(equipmentFault.equipment_id)
            print(Equipment.query.filter_by(equipment_id=equipmentFault.equipment_id).first())
            data = Equipment.query.filter_by(equipment_id=equipmentFault.equipment_id).first()
            equipmentFaults_list.append(
                {'sequence_id': equipmentFault.sequence_id,
                 'sequence_name': equipmentFault.sequence_name,
                 'failure_date': equipmentFault.failure_date,
                 'fault_message': equipmentFault.fault_message,
                 'person': equipmentFault.person,
                 'Equipment': data.get() if data else []
                 }
            )
        pagination_info = {
            'current_page': equipmentFaults.page,
            'total_pages': equipmentFaults.pages,
            'items_per_page': equipmentFaults.per_page,
            'total_items': equipmentFaults.total
        }
        return {
            'users': equipmentFaults_list,
            'pagination': pagination_info
        }


@app.route('/selectEquipmentFaultById', methods=['GET'])
def selectEquipmentFaultById():
    if request.method == 'GET':
        sequence_id = request.args.get('sequence_id')
        # 进行分页查询
        equipmentFault = Equipment_fault.query.filter_by(sequence_id=sequence_id).first()
        data = Equipment.query.filter_by(equipment_id=equipmentFault.equipment_id).first()
        info = {'sequence_id': equipmentFault.sequence_id,
                'sequence_name': equipmentFault.sequence_name,
                'failure_date': equipmentFault.failure_date,
                'fault_message': equipmentFault.fault_message,
                'person': equipmentFault.person,
                'Equipment': data.get() if data else []
                }
        return jsonify(info)
